<template>
  <div class="action-sheet-demo">
    <div class="action-sheet-trigger">
      <button @click="showActionSheet = true">{{ btnName }}</button>
    </div>

    <div class="switch-list">
      <div v-for="(item, index) in swtchList" :key="index" class="flex_center choose_box">
        <div class="c_name">{{ item.title }}</div>
        <div 
          class="switch2" 
          :class="{'switch2--active': item.isChoosed}"
          @click="handleSwitch(index)"
        >
          <div class="circle"></div>
        </div>
      </div>
    </div>

    <Teleport to="body">
      <div v-if="showActionSheet" class="action-sheet-overlay" @click="showActionSheet = false">
        <div class="action-sheet" @click.stop>
          <div 
            v-for="(action, index) in filteredActionList" 
            :key="index"
            class="action-item"
            @click="handleAction(action)"
          >
            {{ action.title }}
          </div>
          <div class="action-item cancel" @click="showActionSheet = false">取消</div>
        </div>
      </div>
    </Teleport>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'

const props = defineProps({
  btnName: {
    type: String,
    default: '按钮'
  },
  actionList: {
    type: Array,
    default: () => [
      { title: '操作1' },
      { title: '操作2' },
      { title: '操作3' },
      { title: '操作4' },
      { title: '操作5' }
    ]
  }
})

const emit = defineEmits(['handleAction'])

const showActionSheet = ref(false)

const swtchList = ref([
  { title: 'Show Option 1', isChoosed: true },
  { title: 'Show Option 2', isChoosed: true },
  { title: 'Show Option 3', isChoosed: true },
  { title: 'Show Option 4', isChoosed: true },
  { title: 'Show Option 5', isChoosed: true }
])

const filteredActionList = computed(() => {
  return props.actionList.filter((_, index) => swtchList.value[index].isChoosed)
})

const handleSwitch = (index) => {
  swtchList.value[index].isChoosed = !swtchList.value[index].isChoosed
}

const handleAction = (action) => {
  emit('handleAction', action)
  showActionSheet.value = false
}
</script>

<style lang="scss" scoped>
.flex_center {
  display: flex;
  align-items: center;
}

.switch2 {
  width: 70px;
  height: 30px;
  border-radius: 20px;
  background-color: #707070;
  cursor: pointer;
  transition: background-color 0.3s;
}

.circle {
  width: 30px;
  height: 30px;
  background-color: #fff;
  border-radius: 50%;
  transition: transform 0.3s;
}

.switch2--active {
  background-color: rgb(82, 231, 132);
  display: flex;
  justify-content: flex-end;
}

.choose_box {
  padding-left: 40px;
  margin: 40px 0;
}

.c_name {
  width: 300px;
  margin-right: 50px;
}

.action-sheet-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: flex-end;
  z-index: 1000;
}

.action-sheet {
  width: 100%;
  background-color: #fff;
  border-radius: 16px 16px 0 0;
  overflow: hidden;
}

.action-item {
  padding: 16px;
  text-align: center;
  border-bottom: 1px solid #eee;
  cursor: pointer;
  
  &:hover {
    background-color: #f5f5f5;
  }
  
  &.cancel {
    color: #999;
    margin-top: 8px;
  }
}

.action-sheet-trigger {
  margin-bottom: 20px;
  
  button {
    padding: 8px 16px;
    background-color: #1c6bba;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    
    &:hover {
      background-color: #1557a0;
    }
  }
}
</style>